package com.demo.personalaccountingbackend.controller;

import com.demo.personalaccountingbackend.dto.ApiResponse;
import com.demo.personalaccountingbackend.entity.Category;
import com.demo.personalaccountingbackend.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类描述信息
 *
 * @author zgx
 * @version 1.0
 * @date 2025/10/15 16:15
 */

@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @Operation(summary = "获取所有分类的树形结构")
    @GetMapping("/tree")
    public List<Category> getCategoryTree() {
        return categoryService.getCategoryTree();
    }

    @Operation(summary = "根据类型获取分类树形结构")
    @GetMapping("/tree/{type}")
    public List<Category> getCategoryTreeByType(
            @Parameter(description = "分类类型：0=支出，1=收入,2=转账，3=借贷")
            @PathVariable Integer type) {
        return categoryService.getCategoryTreeByType(type);
    }

    /**
     * 获取分类列表
     */
    @GetMapping("/list")
    public ApiResponse getCategories(@RequestParam(required = false) Integer type) {
        return categoryService.getCategories(type);
    }

    /**
     * 获取分类详情
     */
    @GetMapping("/{id}")
    public ApiResponse getCategoryDetail(@PathVariable Long id) {
        return categoryService.getCategoryDetail(id);
    }

    /**
     * 添加分类
     */
    @PostMapping("/add")
    public ApiResponse addCategory(@Valid @RequestBody Category category) {
        return categoryService.addCategory(category);
    }

    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public ApiResponse updateCategory(@PathVariable Long id, @Valid @RequestBody Category category) {
        return categoryService.updateCategory(id, category);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public ApiResponse deleteCategory(@PathVariable Long id) {
        return categoryService.deleteCategory(id);
    }
}
